package com.zsyh.jiujiu.adepter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;

import com.lwb.retrofit.RetrofitTask;
import com.ocnyang.cartlayout.viewholder.CartViewHolder;
import com.zsyh.jiujiu.Api;
import com.zsyh.jiujiu.R;
import com.zsyh.jiujiu.bean.GoodsBean;
import com.zsyh.jiujiu.net.MyFragementObserver;
import com.zsyh.jiujiu.net.MyObserver;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import retrofit2.http.POST;


public abstract class ChildViewHolder extends CartViewHolder implements View.OnClickListener {
    public ImageView textViewReduce;
    public TextView textView;
    public TextView textViewPrice;
    public TextView textViewNum;
    public ImageView textViewAdd;
    public ImageView logo;

    FragmentActivity mContext;
    public ChildViewHolder(View itemView, int chekbox_id, FragmentActivity mContext) {
        super(itemView, chekbox_id);

        this.mContext=mContext;
        textView = itemView.findViewById(R.id.huanrugoodname);
        textViewPrice = itemView.findViewById(R.id.price);
        textViewReduce = ((ImageView) itemView.findViewById(R.id.jian));
        textViewNum = itemView.findViewById(R.id.huanrunum);
        textViewAdd = itemView.findViewById(R.id.jia);
        logo=itemView.findViewById(R.id.huanrulogo);
        itemView.setOnClickListener(this);
        textViewReduce.setOnClickListener(this);
        textViewAdd.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.item:
                Toast.makeText(v.getContext(), ((GoodsBean) mICartItem).getGoods_name(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.jian:
                int intValue = Integer.valueOf(textViewNum.getText().toString()).intValue();
                if (intValue > 1) {
                    intValue--;
                    textViewNum.setText(String.valueOf(intValue));
                    ((GoodsBean) mICartItem).setGoods_amount(intValue);
                    onNeedCalculate();
                    modifynumber(((GoodsBean) mICartItem).getCarid(),String.valueOf(intValue));
                }
                break;
            case R.id.jia:
                int intValue2 = Integer.valueOf(textViewNum.getText().toString()).intValue();
                intValue2++;
                textViewNum.setText(String.valueOf(intValue2));
                ((GoodsBean) mICartItem).setGoods_amount(intValue2);
                onNeedCalculate();
                modifynumber(((GoodsBean) mICartItem).getCarid(),String.valueOf(intValue2));
                break;
            default:
                break;
        }
    }

    /**
     * 这里因为把 ViewHolder 没有写到 adapter 中作为内部类，所以对事件写了一个回调的抽象方法。
     * 如果不想这样写，你可以在以下方式中选其一：
     * 1. 将 ViewHolder 写到 Adapter 中作为内部类，这样你就可以访问 Adapter 中的一些方法属性了；
     * 2. 或者，你把 ItemView & ItemChildView 的事件放到 Adapter 中的 onBindViewHolder() 方法中设置。
     */
    public abstract void onNeedCalculate();


    /**
     * 修改数量
     */
    public void modifynumber(String id,String num){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .updateCar(id,num)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyFragementObserver(mContext,true) {
                    @Override
                    protected void onError(String errorMsg) {
                        Log.e("shuliangerror",errorMsg);
                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("shuliang",JsonStr);

                    }
                });

    }


}
